package com.yonyou.pmclouds.print.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.print.client.business.PrintServer;
import com.yonyou.iuap.print.client.utils.JsonUtil;
import com.yonyou.iuap.print.client.utils.PrintUrlUtil;
import com.yonyou.iuap.utils.HttpTookit;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.businessobject.rmiitf.BusinessObjectMaintain;
import com.yonyou.pmclouds.print.rmiitf.PrintMaintain;
//import com.yonyou.pmclouds.print.util.FileConverterUtils;
//import com.yonyou.pmclouds.print.util.WordUtil;
//import org.apache.commons.lang3.StringUtils;
//import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.springframework.beans.factory.annotation.Autowired;

//import java.io.ByteArrayInputStream;
//import java.io.IOException;
import java.io.InputStream;
//import java.util.ArrayList;
import java.util.HashMap;
//import java.util.List;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = PrintMaintain.class, timeout = 5000)
public class PrintMaintainImpl implements PrintMaintain {

    @Autowired
    private BusinessObjectMaintain businessObjectMaintain;

    @Override
    public String doPrintPost(Map<String, String> paramsMap) throws BusinessException {
        try {
            String url = paramsMap.remove("url");
            String tenantId = paramsMap.remove("tenantId");
            return PrintServer.execPost(url, paramsMap, tenantId, null);
        } catch (Exception e) {
            throw new BusinessException("send post request : " + e.getMessage());
        }
    }

    @Override
    public String doPrintGet(Map<String, String> paramsMap) throws BusinessException {
        try {
            String url = paramsMap.remove("url");
            String tenantId = paramsMap.remove("tenantId");
            return PrintServer.execGet(url, paramsMap, tenantId, null);
        } catch (Exception e) {
            throw new BusinessException("send get request : " + e.getMessage());
        }
    }

    @Override
    public String getPrintDesign(Map<String, String> paramMap) throws BusinessException {
        String url = PrintUrlUtil.getdesignUrl();
        return HttpTookit.doGet(url, paramMap);
    }

    @Override
    public String getPrintPreview(Map<String, String> paramMap) throws BusinessException {
        String url = PrintUrlUtil.getpreviewUrl();
        return HttpTookit.doGet(url, paramMap);
    }

    @Override
    public String getPrintData(Map<String, String> paramsMap, String realUrl) throws BusinessException {
        String ret = "";
        String params = paramsMap.remove("params");
        paramsMap = this.jsonStrToMap(params);
        String getDataUrl = paramsMap.remove("getDataUrl");
        if(getDataUrl != null && !getDataUrl.startsWith("/")) {
            getDataUrl = realUrl + "/" + getDataUrl;
        }else{
            getDataUrl = realUrl + getDataUrl;
        }
        String method = paramsMap.remove("method");
        if ("GET".equalsIgnoreCase(method)) {
            ret = HttpTookit.doGet(getDataUrl, paramsMap);
        } else if ("POST".equalsIgnoreCase(method)) {
            ret = HttpTookit.doPost(getDataUrl, paramsMap);
        }
        Map<String, Object> jsonMap = JsonUtil.jsonToMap(ret);
        Object result = jsonMap.get("result");
        return JSON.toJSONString(result);
    }

    @Override
    public String getPrintDelegate(Map<String, String> paramsMap, String method) throws BusinessException {
        String realUrl = paramsMap.remove("realurl");
        String tenantId = paramsMap.remove("tenantId");
        if ("POST".equalsIgnoreCase(method)) {
            return PrintServer.execPost(realUrl, paramsMap, tenantId, "");
        } else {
            return PrintServer.execGet(realUrl, paramsMap, tenantId, "");
        }
    }



    @Override
    public InputStream exportFile(Map<String, Object> paramsMap, String tenantId) throws BusinessException {
        /**
         * 输出文档暂时注释掉
        Map<String, String> printCodeMap = new HashMap<>();
        List<InputStream> inputStreamList = new ArrayList<>();
        for(Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            String boCode = entry.getKey();
            String printCode = printCodeMap.get(boCode);
            if(StringUtils.isEmpty(printCode)) {
                printCode = businessObjectMaintain.getPrintCode(boCode, tenantId);
                printCodeMap.put(boCode, printCode);
            }
            JSONObject jsonObject = (JSONObject) entry.getValue();
            for(Map.Entry<String, Object> fieldEntry : jsonObject.entrySet()) {
                String url = getPreviewUrl(fieldEntry, boCode);
                String htmlContent = getPreviewHtml(url);
                inputStreamList.add(getDocStream(htmlContent));
            }
        }
        WordUtil wordUtil = new WordUtil();
        try {
            return wordUtil.mergeDocx(inputStreamList);
        } catch (IOException | Docx4JException e) {
            throw new BusinessException("Merge web documents :" + e);
        }
         *
         */
        return null;
    }

    /**
     *
    private String getPreviewUrl(Map.Entry<String, Object> entry, String boCode){
        StringBuilder stringBuilder = new StringBuilder();
        String fieldCode = entry.getKey();
        String value = (String)entry.getValue();
        return null;
    }

    private String getPreviewHtml(String url) {
        return null;
    }

    private ByteArrayInputStream getDocStream(String htmlContent) throws BusinessException{
        FileConverterUtils fileConverterUtils = new FileConverterUtils();
        ByteArrayInputStream byteArrayInputStream = fileConverterUtils.convertHtmlToDocx(htmlContent);
        return byteArrayInputStream;
    }
     *
     */

    private Map<String,String> jsonStrToMap(String jsonStr){
        Map<String,String > map = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        for(Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            map.put(key, value);
        }
        return map;
    }
}
